package com.jwell.panprovider.ui.activity

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import android.view.View
import android.view.WindowManager
import android.widget.ImageView
import android.widget.TextView
import com.jwell.cargoapp.ui.base.MVPBaseActivity
import com.jwell.panprovider.R
import com.jwell.panprovider.application.App
import com.jwell.panprovider.contract.ElectronicPoundContract
import com.jwell.panprovider.entity.ElectronicePoundBean
import com.jwell.panprovider.entity.MeasurePlace
import com.jwell.panprovider.model.ElectronicPoundModel
import com.jwell.panprovider.presenter.ElectronicPoundPresenter
import com.jwell.panprovider.tools.DataModel
import com.jwell.panprovider.ui.adapter.ElectronicAdapter
import com.jwell.panprovider.ui.custom.DateChooseWheelViewDialog
import com.jwell.panprovider.ui.dialog.ChooseStringDialog
import kotlinx.android.synthetic.main.activity_electronic_pound_search.*
import kotlinx.android.synthetic.main.empty_layout.*
import kotlinx.android.synthetic.main.error_layout.*
import kotlinx.android.synthetic.main.list_layout.*
import kotlinx.android.synthetic.main.toolbar.*

/**
 * 电子磅单搜索
 */
class ElectronicPoundSearchActivity : MVPBaseActivity<ElectronicPoundPresenter>(), ElectronicPoundContract.View, View.OnClickListener {


    private var haveSearch = false
    private var searchData = ElectronicPoundModel.RequestData()
    private var listData = ArrayList<ElectronicePoundBean>()
    private var adapter = ElectronicAdapter(listData)
    override fun createPresenter(): ElectronicPoundPresenter? {
        return ElectronicPoundPresenter()
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE)
        setContentView(R.layout.activity_electronic_pound_search)
        initToolBar("磅单搜索", true, null)
        toolbar_right_text_save.text = "搜索"
        toolbar_right_text_save.visibility = View.VISIBLE
        setLabel2(true, false, false)
        setListener()
        initData()
    }

    private fun initData() {
        initEmptyAndErrorView()
        loadingLayout.visibility = View.GONE
        recyclerView.layoutManager = LinearLayoutManager(this)
        recyclerView.adapter = adapter
        freshLayout.setEnableLoadmoreWhenContentNotFull(true)
        freshLayout.setEnableFooterFollowWhenLoadFinished(true)
        freshLayout.setOnRefreshListener {
            presenter?.fresh()
        }
        freshLayout.setOnLoadmoreListener {
            presenter?.load()
        }
        presenter?.getMeasurePlaceList()
    }

    private fun initEmptyAndErrorView() {
        image_empty.setImageResource(R.mipmap.icon_waybill)
        text_empty_describe.text = "无相关电子磅单数据"
        ll_error.setOnClickListener {
            errorLayout.visibility = View.GONE
            loadingLayout.visibility = View.VISIBLE
            presenter?.fresh()
        }
    }

    override fun setSearchTotalCount(count: Int) {
        if (count > 0) {
            initToolBar("搜索到" + count + "条磅单", true, null)
        } else {
            initToolBar("未搜索到相关磅单", true, null)
        }
    }

    private fun setListener() {
        toolbar_right_text_save.setOnClickListener(this)
        rl_item21.setOnClickListener(this)
        rl_item22.setOnClickListener(this)
        rl_item23.setOnClickListener(this)
        text_time1.setOnClickListener(this)
        text_time2.setOnClickListener(this)
        text_choose.setOnClickListener(this)
    }


    private fun setLabel2(c1: Boolean, c2: Boolean, c3: Boolean) {
        rl_item21.isSelected = c1
        rl_item22.isSelected = c2
        rl_item23.isSelected = c3
        setTextColor(c1, text_label21, image_tick_21)
        setTextColor(c2, text_label22, image_tick_22)
        setTextColor(c3, text_label23, image_tick_23)
    }


    private fun setTextColor(isChecked: Boolean, textView: TextView, imageView: ImageView) {
        when (isChecked) {
            true -> {
                textView.setTextColor(application.resources.getColor(R.color.black))
                imageView.visibility = View.VISIBLE
            }
            false -> {
                textView.setTextColor(application.resources.getColor(R.color.c999999))
                imageView.visibility = View.GONE
            }
        }
    }

    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.text_choose -> {//选择计料点
                var unLoadingPintList: List<MeasurePlace>? = null
                DataModel.getInstance().measurePlace?.let {
                    unLoadingPintList = it
                }
                var stringList = mutableListOf<String>()
                unLoadingPintList?.let {
                    it.mapTo(stringList) { it.caR_MEASURE_PLACE_DESCR }
                }
                if (stringList.isEmpty()) {
                    toast("未获取到计量点信息")
                    return
                }
                var dialog1 = ChooseStringDialog.Builder(this, stringList)
                        .setOnclickListener { text ->
                            edit_measure_address.setText(text)
                        }
                        .creat()
                dialog1.show()
            }
            R.id.text_time1 -> {//选择开始时间
                val startDateChooseDialog = DateChooseWheelViewDialog(this@ElectronicPoundSearchActivity, DateChooseWheelViewDialog.DateChooseInterface { time, longTimeChecked ->
                    text_time1.text = time

                })
                startDateChooseDialog.setDateDialogTitle("供量开始时间")
                startDateChooseDialog.setTimePickerGone(true)
                startDateChooseDialog.showDateChooseDialog()
            }
            R.id.text_time2 -> {//选择结束时间
                val startDateChooseDialog = DateChooseWheelViewDialog(this@ElectronicPoundSearchActivity, DateChooseWheelViewDialog.DateChooseInterface { time, longTimeChecked ->
                    text_time2.text = time

                })
                startDateChooseDialog.setDateDialogTitle("供量结束时间")
                startDateChooseDialog.setTimePickerGone(true)
                startDateChooseDialog.showDateChooseDialog()
            }
            R.id.toolbar_right_text_save -> {
                searchData = ElectronicPoundModel.RequestData()
                when (toolbar_right_text_save.text) {
                    "重新搜索" -> {
                        list_layout.visibility = View.GONE
                        scrollView_search.visibility = View.VISIBLE
                        haveSearch = false
                    }
                    "搜索" -> {

                        if (rl_item21.isSelected) {
                            searchData.type = 3
                        }
                        if (rl_item22.isSelected) {
                            searchData.type = 1
                        }
                        if (rl_item23.isSelected) {
                            searchData.type = 2
                        }
                        searchData.measurementType = 3
                        searchData.forBillCode = edit_code.text.toString()
                        searchData.driverName = edit_driverName.text.toString()
                        searchData.carNum = edit_carNum.text.toString()
                        searchData.materialName = edit_goodsName.text.toString()
                        searchData.forwardingUnit = edit_sendCom.text.toString()
                        searchData.receivingUnit = edit_receiptCom.text.toString()
                        searchData.HEAVY_CAR_WEIGHT_ROOM = edit_roomNum.text.toString()
                        searchData.weightNo = edit_poundNum.text.toString()
                        searchData.carMeasurePlaceDescr = edit_measure_address.text.toString()
                        searchData.carMeasureByDescr = edit_measure_person.text.toString()
                        searchData.startDate = text_time1.text.toString()
                        searchData.endDate = text_time2.text.toString()
                        searchData.skipCount = 0
                        showProgress("", true)
                        presenter?.searchListData(searchData)
                        haveSearch = true
                        list_layout.visibility = View.VISIBLE
                        scrollView_search.visibility = View.GONE
                    }
                }
                when (haveSearch) {
                    true -> {
                        toolbar_right_text_save.text = "重新搜索"
                    }
                    false -> {
                        toolbar_right_text_save.text = "搜索"
                        initToolBar("磅单搜索", true, null)
                    }
                }
            }
            R.id.rl_item21 -> setLabel2(true, false, false)
            R.id.rl_item22 -> setLabel2(false, true, false)
            R.id.rl_item23 -> setLabel2(false, false, true)

        }
    }

    override fun showListData(data: List<ElectronicePoundBean>, isFresh: Boolean) {
        loadingLayout.visibility = View.GONE
        errorLayout.visibility = View.GONE
        emptyLayout.visibility = View.GONE
        if (isFresh) {
            listData.clear()
            freshLayout.finishRefresh()
            //设置可以加载更多
            freshLayout.resetNoMoreData()
            if (App.DEBUG) println("设置可以加载更多")
        } else {
            freshLayout.finishLoadmore()
        }
        listData.addAll(data)
        adapter.notifyDataSetChanged()
        if (listData.size == 0) {
            emptyLayout.visibility = View.VISIBLE
        } else if (data.size < 10) {
            freshLayout.finishLoadmoreWithNoMoreData()
            if (App.DEBUG) println("设置不可以加载更多")
        }
    }

    override fun showListDataFailed(isFresh: Boolean, str: String) {
        loadingLayout.visibility = View.GONE
        errorLayout.visibility = View.GONE
        emptyLayout.visibility = View.GONE
        if (isFresh) {
            freshLayout.finishRefresh(false)
            if (listData.size == 0) errorLayout.visibility = View.VISIBLE
            freshLayout.finishLoadmore(false)
        } else {
            freshLayout.finishLoadmore(false)
        }
    }
}

